package com.wangcang.widgetlibrary.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

import com.wangcang.widgetlibrary.R;

/**
 * notification 通知工具类
 * Created by ShineF on 2017/6/19 0019.
 */

public class NotificationTools {

    private static NotificationTools mNotification;
    private int i = 0;
    private Context mContext = null;
    private int currentI = 0;


    public static NotificationTools getInstance(Context context) {
        if (null == mNotification) {
            synchronized (NotificationTools.class) {
                if (null == mNotification) {
                    mNotification = new NotificationTools(context);
                }
            }
        }
        return mNotification;
    }

    private NotificationTools(Context context) {
        this.mContext = context;

    }

    /**
     * 通知系统默认样式
     *
     * @param value   传递的参数
     * @param title   标题
     * @param content 内容
     * @param clazz   跳转的类
     */
    public void sendNotification(String title, String value, String content, Class clazz) {
        NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(mContext, clazz);
        intent.putExtra("content", value);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher))
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentTitle(title)
                .setContentText(content)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
//                .setPriority(NotificationCompat.PRIORITY_MAX);
//        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
//            builder.setFullScreenIntent(pendingIntent, true);
//        }
        manager.notify(1, builder.build());
    }


    /**
     * 自定义Notification
     *
     * @param text  通知内容
     * @param clazz 跳转的页面
     */
    public void sendCustomNotification(String title, String text, Class clazz) {
        Intent intent = new Intent(mContext, clazz);
        intent.putExtra("content", text);
        currentI = i++;
        NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, currentI, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews mRemoteView = new RemoteViews(mContext.getPackageName(), R.layout.view_notification_type);
        mRemoteView.setTextViewText(R.id.tv_text, title);
        mRemoteView.setImageViewResource(R.id.iv_icon, R.drawable.ic_launcher);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher))
                .setDefaults(Notification.DEFAULT_ALL)
                .setContent(mRemoteView)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
//                .setPriority(NotificationCompat.PRIORITY_MAX);
//        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
//            builder.setFullScreenIntent(pendingIntent, true);
//        }
        manager.notify(currentI, builder.build());
    }

}
